#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std; 
typedef long long LL;
LL Erfen(LL n, LL y) {
	double left=0, right=n+10;
	LL cnt=0;
	LL r=0;
	while (y) {
		r=r*10+y%10;
		y=y/10;
		++cnt;
	}
//	cout<<"r: "<<r<<endl;
	while (left+1<right) {
		double mid=(left+right)/2;
		double t=0;
		for (LL i=0,c=r; i<cnt; ++i, c/=10) {
			t=t*mid+c%10;
			if (t>n) break;
		}
//		cout<<left<<" vs "<<right<<endl; 
		if (t>n) right=mid;
		else left=mid;
	}
	return right+1;
}
bool check(LL n, LL res, LL y) {
	LL t=n;
	bool flag=true;
	LL cp=0, cnt=0; 
//	cout<<"t: "<<t<<endl;
	while (t) {
		if (t%res>9) {
			flag=false;
			break;
		}
		cp=cp*10+t%res;
		++cnt;
		t=t/res;
	}
	LL cp1=cp;
	cp=0;
	while (cnt--) {
		cp=cp*10+cp1%10;
		cp1/=10;
	}
//	cout<<"--- "<<res<<endl;
//	cout<<cp<<" vs "<<n<<endl;
	if (cp<y) flag=false;
	return flag;
}
int main() {
	LL n,y;
	while (cin>>n>>y) {
		LL res;
		bool mark=false;
		while (y<=3000) {
			res=Erfen(n, y);
			if (check(n, res, y)) {
				mark=true;
				break;
			}
			--res;
			if (check(n, res, y)) {
				mark=true;
				break;
			}
			--res;
			if (check(n, res, y)) {
				mark=true;
				break;
			}
//			cout<<"-------------"<<y<<endl;
			++y;
		}
		if (mark) cout<<(LL)res<<endl;
		else {
			res=Erfen(n, y);
//			cout<<"Erfen: "<<res<<endl;
			while (res) {
				if (check(n, res, y)) break;
//				cout<<"-----------"<<res<<endl;
				--res;
			}
			cout<<(LL)res<<endl;
		}
	}
	return 0;
}
/* 
1000000000000000000 100000
*/ 
